package com.yunxi.controller.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.AreaVO;
import com.yunxi.sys.service.AreaDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地域信息 前端控制器
 * </p>
 *
 * @author
 * @since 2019-05-07
 */
@RestController
@RequestMapping("/admin/area")
@Api(tags = "sys:area", description = "地域信息接口")
public class AreaController {
   private static final Logger logger = LoggerFactory.getLogger(AreaController.class);

   @Autowired
   AreaDubboService areaDubboService;


   @GetMapping("/page")
   @ApiOperation(value = "查询分页")
   public ApiResult page(AreaVO areaVO, Integer pageNum, Integer pageSize) throws Exception {
      return ResponseUtil.ok(areaDubboService.pageByVO(areaVO, pageNum, pageSize));
   }

   @GetMapping("/load")
   @ApiOperation(value = "读取单个")
   public ApiResult load(AreaVO areaVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(areaDubboService.getById(areaVO.getAreaId()));
   }

   @GetMapping("/loadCas")
   @ApiOperation(value = "读取单个归属地区/管辖范围")
   public ApiResult loadCas(AreaVO areaVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(areaDubboService.loadCas(areaVO));
   }

   @PostMapping("/save")
   @ApiOperation(value = "保存信息")
   public ApiResult save(AreaVO areaVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      areaDubboService.saveOrUpdate(areaVO);
      return ResponseUtil.ok("success");
   }

   @PostMapping("/saveBatch")
   @ApiOperation(value = "保存归属地区")
   public ApiResult saveBatch(@RequestBody AreaVO areaVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      areaDubboService.saveBatch(areaVO);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/listAllProvince")
   @ApiOperation(value = "查询该大区经理的归属地区对应的所有省")
   public ApiResult listAllProvince(AreaVO areaVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return areaDubboService.listAllProvince(areaVO,loginInfo);
   }

   @GetMapping("/listByVo")
   @ApiOperation(value = "查询列表")
   public ApiResult list(AreaVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(areaDubboService.listByVo(v,loginInfo));
   }

   @GetMapping("/list")
   @ApiOperation(value = "查询")
   public ApiResult list(@RequestParam Map<String,String> params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return  ResponseUtil.ok(areaDubboService.listByPid(params.get("pid")));
   }

   @PostMapping("/del")
   @ApiOperation(value = "删除单个")
   public ApiResult del(@RequestBody AreaVO areaVO) throws Exception {
      areaDubboService.removeById(areaVO.getAreaId());
      return ResponseUtil.ok("success");
   }

   @PostMapping("/delCas")
   @ApiOperation(value = "删除大区和大区下的省份")
   public ApiResult delCas(@RequestBody List<AreaVO> areaVOList) throws Exception {
      areaDubboService.delCas(areaVOList);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/treeByPid")
   @ApiOperation(value = "组织树形查询")
   public ApiResult treeByPid(String pid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return null;
   }

   @GetMapping("/listProvince")
   @ApiOperation(value = "组织树形查询")
   public ApiResult listProvince() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return (ApiResult)areaDubboService.listProvince();
   }

   @GetMapping("/getByProvinceCode")
   @ApiOperation(value = "根据省代码查询对应归属大区")
   public ApiResult getByProvinceCode(String provinceCode) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return ResponseUtil.ok(areaDubboService.getByProvinceCode(provinceCode));
   }

   @GetMapping("/queryAreaByPartnerCode")
   @ApiOperation(value = "根据渠道商编码查询省份")
   public ApiResult queryAreaByPartnerCode(String partnerCode){
      return ResponseUtil.ok(areaDubboService.queryAreaByPartnerCode(partnerCode));
   }

/*@PostMapping("/export")
@ApiOperation(value = "导出excel")
public Object export(@RequestParam Map<String,Object> params) throws Exception {
return ResponseUtil.ok(areaDubboService.findMapByList(params));
}*/
}
